package com.xingkeduo.controller.base;

import static com.xingkeduo.configuration.WechatFilter.operateIdThreadLocal;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import com.xingkeduo.model.param.common.BaseParam;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.pojo.exception.InputParamException;

/**
 * 文件概述
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 19-1-10 下午4:49
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
public class InitController extends BaseController {

	protected String errMsg(BindingResult bindingResult) {
		StringBuffer stringBuffer = new StringBuffer();
		if (bindingResult.hasErrors()) {
			for (ObjectError objectError : bindingResult.getAllErrors()) {
				// 收集错误信息
				stringBuffer.append(objectError.getDefaultMessage());
			}
			throw new InputParamException(InputParamException.Type.INVALID, stringBuffer.toString());
		}
		return null;
	}

	protected Long initUserInParams() {
		return initUserInParams(null);
	}

	protected Long initUserInParams(BaseParam baseParam) {
		Long userId = operateIdThreadLocal.get();
		if (userId == null) {
			throw new DataException(DataException.Type.NULL, "用户未登陆");
		}
		if (baseParam != null) {
			baseParam.setCreateBy(userId);
		}
		return userId;
	}

	/**
	 * 根据spring binding 错误信息自定义返回错误码和错误信息
	 *
	 * @param fieldError
	 * @return
	 */
	/*
	 * private CommonResult processBindingError(FieldError fieldError) { String code
	 * = fieldError.getCode(); LOGGER.debug("validator error code: {}", code);
	 * switch (code) { case "NotEmpty": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_EMPTY.getCode(),
	 * fieldError.getDefaultMessage()); case "NotBlank": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_EMPTY.getCode(),
	 * fieldError.getDefaultMessage()); case "NotNull": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_EMPTY.getCode(),
	 * fieldError.getDefaultMessage()); case "Pattern": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Min": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Max": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Length": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Range": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Email": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "DecimalMin": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "DecimalMax": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Size": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Digits": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Past": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); case "Future": return
	 * ResultUtil.returnError(ErrorCodeEnum.PARAM_ERROR.getCode(),
	 * fieldError.getDefaultMessage()); default: return
	 * ResultUtil.returnError(ErrorCodeEnum.UNKNOWN_ERROR); } }
	 */
}
